package com.sc.nft.entity.vo;

import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.enums.CouponsStatusEnum;
import com.sc.nft.enums.CouponsTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.util.BigDecimalUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * EditActivityCouponsVO  (编辑优惠券活动VO)
 *
 * @author ligen
 */
@Data
public class EditActivityCouponsVO {

    @ApiModelProperty(value = "主键ID")
    Long id;

    /**
     * 活动标题
     */
    @NotBlank(message = "活动标题不能为空！")
    @ApiModelProperty(value = "活动标题")
    String title;

    /**
     * 优惠券类型
     */
    @NotNull(message = "优惠券类型不能为空！")
    @ApiModelProperty(value = "优惠券类型")
    CouponsTypeEnum couponsType;

    /**
     * 优惠券满足金额
     */
    @NotNull(message = "优惠券满足金额不能为空！")
    @ApiModelProperty(value = "优惠券满足金额")
    private BigDecimal fullMoney;

    /**
     * 减免金额
     */
    @NotNull(message = "减免金额不能为空！")
    @ApiModelProperty(value = "减免金额（当优惠券类型为 DISCOUNT_COUPON 填写）")
    private BigDecimal money;
    /**
     * 优惠券折扣
     */
    @NotNull(message = "优惠券折扣不能为空！")
    @ApiModelProperty(value = "优惠券折扣（当优惠券类型为 DISCOUNT_PROPORTION_COUPON 填写）")
    private BigDecimal proportion;

    /**
     * 活动开始时间
     */
    @NotNull(message = "活动开始时间不能为空！")
    @ApiModelProperty(value = "活动开始时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    /**
     * 活动结束时间
     */
    @NotNull(message = "活动结束时间不能为空！")
    @ApiModelProperty(value = "活动结束时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    /**
     * 领券后多少天生效
     */
    @ApiModelProperty(value = "领券后多少天生效")
    private Integer afterDay;

    /**
     * 有效天数
     */
    @NotNull(message = "有效天数不能为空！")
    @ApiModelProperty(value = "有效天数")
    private Integer validDay;

    /**
     * 活动商品ID
     */
    @NotNull(message = "活动商品ID不能为空！")
    @ApiModelProperty(value = "活动商品ID")
    private List<Long> productIdList;

    /**
     * 优惠券活动状态
     */
    @ApiModelProperty(value = "优惠券活动状态")
    private CouponsStatusEnum status;

    /**
     * 排序字段
     */
    private Integer sort;

    public void check(){
        if (Objects.isNull(couponsType)) {
            throw new GlobalRunTimeException(ErrorCode.CHOOSE_DISCOUNT_TYPE);
        }
        // 活动开始时间不得大于结束时间
        if (DateTime.of(startTime).isAfter(endTime)){
            throw new GlobalRunTimeException(ErrorCode.THE_END_TIME_MUST_BE_NO_LATER_THAN_THE_START_TIME);
        }
        if (DateTime.now().after(startTime)){
            throw new GlobalRunTimeException(ErrorCode.THE_START_TIME_CANNOT_BE_EARLIER_THAN_THE_CURRENT_TIME);
        }
        // 优惠券金额校验
        //满减
        if (couponsType.getType().equals(CouponsTypeEnum.DISCOUNT_COUPON.getType())) {
            if (BigDecimalUtils.compare(money,fullMoney) || !BigDecimalUtils.greaterEqZero(money) || !BigDecimalUtils.greaterEqZero(fullMoney)) {
                throw new GlobalRunTimeException(ErrorCode.THE_AMOUNT_OF_DEDUCTION_SHOULD_NOT_BE_GREATER_THAN_THE_AMOUNT_SPENT);
            }
        }else if (couponsType.getType().equals(CouponsTypeEnum.DISCOUNT_PROPORTION_COUPON.getType())) {
            if (( BigDecimalUtils.compare(proportion,10) || BigDecimalUtils.compare(0,proportion))) {
                throw new GlobalRunTimeException(ErrorCode.COUPONS_DISCOUNT_PROPORTION_RANGE);
            }
        }
        if (CollectionUtils.isEmpty(productIdList) || productIdList.size() == 0){
            throw new GlobalRunTimeException("活动商品ID不能为空！");
        }
        if (Objects.isNull(afterDay)){
            afterDay = 0;
        }
    }
}
